﻿using AutoMapper;
using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.InterfaceService;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.IRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Service
{
    public class DetailService : IDetailService
    {
        #region
        [Autowired]
        private IDetailRepository detailRepository { get; set; }
        [Autowired]
        private  IMapper mapper { get; set; }

        public DetailService()
        {
        }
        #endregion

        #region
        public async Task<List<DetailEntity>> GetDetails(int MainID)
        {
            List<DetailEntity> details = await detailRepository.Where(d => d.MainID == MainID).ToListAsync();
            return details;
        }

        public async Task<List<DetailEntity>> GetDetails(int MainID, int ParentID)
        {
            List<DetailEntity> details = await detailRepository.Where(d => d.MainID == MainID && d.ParentID == ParentID).ToListAsync();

            return details;
        }

        public async Task<List<DetailEntity>> Find(int ID)
        {
            List<DetailEntity> details = await detailRepository.Where(d => d.ID == ID).ToListAsync();
            return details;
        }

        public async Task<bool> AddArea(AddArea addArea)
        {
            DetailEntity entity = mapper.Map<DetailEntity>(addArea);
            bool IsOk = false;

            if (await detailRepository._dbContext.Details.Where(t=>t.RowName==addArea.RowName &&t.MainID==(int)eDictionaryType.Area).AnyAsync())
            {
                throw new Exception("runtime:添加失败,名称已经重复!");
            }
            if (await detailRepository.Where(t => t.ID == addArea.ParentID).AnyAsync())
            {
                int count = await detailRepository.Where(t => t.ParentID == addArea.ParentID).CountAsync();
                count++;
                entity.RowID = count + 1;
                entity.MainID = (int)eDictionaryType.Area;
                entity.RowDesc = "区";
                await detailRepository.Create(entity, 0);
                IsOk = await detailRepository.SaveChangesAsync();

            }
            else
            {
                IsOk = false;
                throw new Exception("runtime:传入市的编号错误!");
            }
            return IsOk;
        }

        public async Task<bool> AddCity(AddArea addArea)
        {
            DetailEntity entity = mapper.Map<DetailEntity>(addArea);
            bool IsOk = false;

            if (await detailRepository._dbContext.Details.Where(t => t.RowName == addArea.RowName &&t.MainID==(int)eDictionaryType.City).AnyAsync())
            {
                throw new Exception("runtime:添加失败,名称已经重复!");
            }

            if (await detailRepository.Where(t => t.ID == addArea.ParentID).AnyAsync())
            {
                int count = await detailRepository.Where(t => t.ParentID == addArea.ParentID).CountAsync();
                count++;
                entity.RowID = count + 1;
                entity.MainID = (int)eDictionaryType.City;
                entity.RowDesc = "市";
                await detailRepository.Create(entity, 0);
                IsOk = await detailRepository.SaveChangesAsync();

            }
            else
            {
                IsOk = false;
                throw new Exception("runtime:传入市的编号错误!");
            }
            return IsOk;
        }

        public async Task<bool> AddProvince(AddArea addArea)
        {

            DetailEntity entity = mapper.Map<DetailEntity>(addArea);
            bool IsOk = false;
            if (await detailRepository._dbContext.Details.Where(t => t.RowName == addArea.RowName && t.MainID==(int)eDictionaryType.Province).AnyAsync())
            {
                throw new Exception("runtime:添加失败,名称已经重复!");
            }

            int count = await detailRepository.Where(t => t.ParentID == 0).CountAsync();
            count++;
            entity.RowID = count + 1;
            entity.MainID = (int)eDictionaryType.Province;
            entity.RowDesc = "省";
            await detailRepository.Create(entity, 0);
            IsOk = await detailRepository.SaveChangesAsync();

            return IsOk;
        }

        public async Task<bool> UpdateArea(AddArea addArea,int ID)
        {
          
            DetailEntity entity = await detailRepository.FirstOrDefaultAsync(t => t.ID == ID);
            mapper.Map(addArea,entity);
            bool IsOk = false; 
            if (await detailRepository._dbContext.Details.Where(t => t.RowName == addArea.RowName && t.ID!=ID).AnyAsync())
            {
             throw new Exception("runtime:更新失败,名称已经重复!");
            }
            await detailRepository.Update(entity,0);
            IsOk=await detailRepository.SaveChangesAsync();
            return IsOk;
        }

        public async Task<PageList<DetailView>> GetDetails(DetailParameter Params)
        {
            var query = detailRepository.GetDetails(t => t.MainID == (int)eDictionaryType.Province ||
            t.MainID == (int)eDictionaryType.Area || t.MainID == (int)eDictionaryType.City);
            return await PageList<DetailView>.CreateAsync(query,Params);
        }
        public async Task<DetailView> GetDetailsByID(int ID)
        {
            var entity =await detailRepository.GetDetails(t=>t.ID==ID &&
            (t.MainID == (int)eDictionaryType.Province ||
            t.MainID == (int)eDictionaryType.Area ||
            t.MainID == (int)eDictionaryType.City)).FirstOrDefaultAsync();
            return entity;
        }


        public async Task<bool> Delete(int ID)
        {
            if(await detailRepository._dbContext.Files.Where(t => t.CityID == ID).AnyAsync())
            {
                throw new Exception("runtime:删除失败上传文件已经绑定该地区！");
            }

            if (await detailRepository._dbContext.Drug.Where(t => t.AreaID==ID).AnyAsync()|| await detailRepository._dbContext.DrugDetail.Where(t => t.AreaID == ID).AnyAsync())
            {
                throw new Exception("runtime:删除失败上传文件已经绑定该地区！");
            }

            if (await detailRepository._dbContext.Company.Where(t => t.AreaID == ID || t.ProvinceID==ID || t.CityID==ID).AnyAsync())
            {
                throw new Exception("runtime:删除失败机构已经绑定该地区！");
            }
            if (await detailRepository._dbContext.Details.Where(t => t.ParentID == ID).AnyAsync())
            {
                throw new Exception("runtime:请先删除该地区的子地区!");
            }

            if (await detailRepository._dbContext.PlaceDutys.Where(t=>t.AreaID==ID || t.CityID==ID || t.ProvinceID==ID).AnyAsync())
            {
                throw new Exception("runtime:该区域已经填写人口,删除失败!");
            }
            await detailRepository.Delete(t=>t.ID==ID);

            return await detailRepository.SaveChangesAsync();
        }

        #endregion
    }
}
